import { Component, OnInit, Input } from '@angular/core';

import { EventService } from 'src/app/shared/service/event.service';
import { HttpService } from 'src/app/shared/service/http.service';

import { AppGlobalService } from './../../shared/service/app-global.service';
declare let cordova;
@Component({
  selector: 'app-pay-success',
  templateUrl: './pay-success.component.html',
  styleUrls: ['./pay-success.component.scss'],
})
export class PaySuccessComponent implements OnInit {
  constructor(
    private appGlobalService: AppGlobalService,
    private http: HttpService,
    private eventService: EventService

  ) { }

  @Input() tagData: any;

  ngOnInit() {
    // this.demoData();
  }
  close() {
    this.appGlobalService.popTarget();
    this.appGlobalService.popTarget();
    this.appGlobalService.popTarget();
    localStorage.setItem('needRefresh', 'true'); // 刷新 页面
    this.eventService.eventEmit.emit('refreshSubTab', 'refreshSubTab'); // 刷新首页
    history.go(-3);
  }
  sureBtn(btn) {

    if (btn.submit) {
      const param = {
        content: btn.submit.content,
        id: btn.submit.target,
        sessionId: localStorage.getItem('sessionId')
      };
      this.http.post("application/submit", param, data => {
      });
    }

    if (btn.link) {
      const param = {
        params: btn.link.params,
        pageId: btn.link.target,
        sessionId: localStorage.getItem('sessionId')
      };
      this.appGlobalService.goToForward(param);
    }
  }

  saveBtn(btn?) {
    console.log('save\n');

    if (btn) {
      if (btn.link) {
        const param = {
          params: btn.link.params,
          pageId: btn.link.target,
          sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(param);
      }
    } else {
      cordova.plugins.Screenshot.saveToAlbum((error, onSuccess) => {
        if (error) {
          console.error(error);
        } else {
          this.appGlobalService.showToast('已保存到手机相册', 1500, "top");
          console.log('success' + onSuccess.filePath);
        }
      });
    }
  }
  finishBtn(btn) {
    console.log('finish\n');
    if (btn.submit) {
      const param = {
        content: btn.submit.content,
        id: btn.submit.target,
        sessionId: localStorage.getItem('sessionId')
      };
      this.http.post("application/submit", param, data => {

      });
    }

    if (btn.link) {
      const param = {
        params: btn.link.params,
        pageId: btn.link.target,
        sessionId: localStorage.getItem('sessionId')
      };
      this.appGlobalService.goToForward(param);
    }


  }
  demoData() {
    this.tagData = {
      type: "tag",
      style: "xxxx",
      name: "xxxx",
      data: {
        iconName: "pay_icon_success@3x.png",
        watermark: "ppp.png",
        title: "支付成功 | 支付失败",
        tips: "您的选址信息已失效",
        bookInfo: [{
          label: "购买服务",
          content: "铺铺旺选址通会员服务（服务一个月）"
        }, {
          label: "支付金额",
          content: "3580元"
        }, {
          label: "支付时间",
          content: "2017-10-10 12:30"
        }, {
          label: "交易单号",
          content: "2017101008088888888"
        }],
        finishButton: {
          label: "完成",
          link: {
            type: "page",
            target: "pageId",
            params: {}
          }
        },
        enterButton: {
          label: "确定",
          link: {
            type: "page",
            target: "pageId",
            params: {}
          }
        },
        saveButton: {
          label: "保存支付凭证"
        }
      }
    };
  }
}
